package com.quanyan.user.service.impl;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.user.mapper.DynamicCommentMapper;
import com.quanyan.user.model.db.TbComment;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.vo.request.ReqNewsComment;
import com.quanyan.user.model.vo.response.ImageObj;
import com.quanyan.user.model.vo.response.RespDynamicComment;
import com.quanyan.user.model.vo.response.RespUserEntity;
import com.quanyan.user.service.DynamicCommentService;
import com.quanyan.user.service.NewsCommentService;
import com.quanyan.user.service.UserDynamicService;
import com.quanyan.user.service.UserQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/17.
 */
@Service
public class DynamicCommentServiceImpl implements DynamicCommentService {

    private static final Logger logger = LoggerFactory.getLogger(DynamicCommentServiceImpl.class);

    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @Autowired
    private UserQueryService userQueryService;

    @Autowired
    private UserDynamicService userDynamicService;

    @Autowired
    private NewsCommentService newsCommentService;

    @Override
    public Page<List<TbComment>> findAllComment(ReqNewsComment reqNewsComment){
        Page page = new Page();
        int pageIndex = reqNewsComment.getPageNum() + 1;
        page.setCurrentPage(pageIndex);
        page.setShowCount(reqNewsComment.getPageSize());
        List<TbComment> lstComment = dynamicCommentMapper.findAllNewsComment(page, reqNewsComment);
        if (pageIndex > page.getTotalPage()){
            lstComment = new ArrayList<>();
            page.setCurrentResult(0);
        }
        page.setObject(lstComment);
        return page;
    }


    @Override
    public APIResponse<PageObj<List<RespDynamicComment>>> findDynamicComment(ReqNewsComment reqNewsComment){
        Page<List<TbComment>> page = this.findAllComment(reqNewsComment);
        List<TbComment> lst =  page.getObject();
        List<RespDynamicComment> lstResult = new ArrayList<>();
        if ( lst != null && lst.size() > 0 ){
            List<Integer> lstUid = new ArrayList<>(); // 评论用户
            for (TbComment tbComment : lst ){
                lstUid.add(tbComment.getUid());
                if (tbComment.getTouid() != null && tbComment.getTouid().intValue() != 0 )
                    lstUid.add(tbComment.getTouid());
            }

            List<TbUser> lstTbUser = userQueryService.batchQueryTbUser(lstUid);
            Map<Integer,TbUser> mapUser = new HashMap<>();
            for (TbUser tbUser : lstTbUser){
                mapUser.put(tbUser.getId(),tbUser);
            }

            for (TbComment tbComment : lst){
                RespDynamicComment respDynamicComment = new RespDynamicComment();
                respDynamicComment.setId(tbComment.getId());
                respDynamicComment.setDynamicDetailsId(tbComment.getModuleId());
                respDynamicComment.setContent(tbComment.getContent());
                respDynamicComment.setCreateTime(tbComment.getCreateTime().getTime());
                respDynamicComment.setType(tbComment.getType());
                respDynamicComment.setToCommentId(tbComment.getToCommentId());
                RespUserEntity user = userDynamicService.convertTbUserToRespUserEntity(mapUser.get(tbComment.getUid()));
                respDynamicComment.setUser(user);
                if (tbComment.getTouid() != null && tbComment.getTouid().intValue() != 0 ){
                    RespUserEntity toUser = userDynamicService.convertTbUserToRespUserEntity(mapUser.get(tbComment.getTouid()));
                    respDynamicComment.setToUser(toUser);
                }else{
                    RespUserEntity respUserEntity = new RespUserEntity();
                    respUserEntity.setIcon(new ImageObj());
                    respDynamicComment.setToUser(respUserEntity);
                }

                lstResult.add(respDynamicComment);
            }
        }

        PageObj<List<RespDynamicComment>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        return APIResponse.returnSuccess(result);
    }


    public List<RespDynamicComment> getRespDynamicComment (ReqNewsComment reqNewsComment){
        Page<List<TbComment>> page = this.findAllComment(reqNewsComment);
        List<TbComment> lst =  page.getObject();
        List<RespDynamicComment> lstResult = new ArrayList<>();
        if ( lst != null && lst.size() > 0 ){
            List<Integer> lstUid = new ArrayList<>(); // 评论用户
            for (TbComment tbComment : lst ){
                lstUid.add(tbComment.getUid());
                if (tbComment.getTouid() != null && tbComment.getTouid().intValue() != 0 )
                    lstUid.add(tbComment.getTouid());
            }

            List<TbUser> lstTbUser = userQueryService.batchQueryTbUser(lstUid);
            Map<Integer,TbUser> mapUser = new HashMap<>();
            for (TbUser tbUser : lstTbUser){
                mapUser.put(tbUser.getId(),tbUser);
            }

            for (TbComment tbComment : lst){
                RespDynamicComment respDynamicComment = new RespDynamicComment();
                respDynamicComment.setId(tbComment.getId());
                respDynamicComment.setDynamicDetailsId(tbComment.getModuleId());
                respDynamicComment.setContent(tbComment.getContent());
                respDynamicComment.setCreateTime(tbComment.getCreateTime().getTime());
                respDynamicComment.setType(tbComment.getType());
                respDynamicComment.setToCommentId(tbComment.getToCommentId());
                RespUserEntity user = userDynamicService.convertTbUserToRespUserEntity(mapUser.get(tbComment.getUid()));
                respDynamicComment.setUser(user);
                if (tbComment.getTouid() != null && tbComment.getTouid().intValue() != 0 ){
                    RespUserEntity toUser = userDynamicService.convertTbUserToRespUserEntity(mapUser.get(tbComment.getTouid()));
                    respDynamicComment.setToUser(toUser);
                }else{
                    RespUserEntity respUserEntity = new RespUserEntity();
                    respUserEntity.setIcon(new ImageObj());
                    respDynamicComment.setToUser(respUserEntity);
                }

                lstResult.add(respDynamicComment);
            }
        }
        return  lstResult;
    }


    @Override
    public Page<List<TbComment>> findAllComment(int moduleType, List<Integer> lstDynamicId) {
        Page page = new Page();
        //page.setCurrentPage(reqNewsComment.getPageNo() + 1);
       // page.setShowCount(reqNewsComment.getPageSize());

        Map<String,Object> map = new HashMap<>();
        map.put("moduleType",moduleType);
        map.put("list",lstDynamicId);
        dynamicCommentMapper.findAllBatchNewsComment(page,map);


        return null;
    }


    public List<RespDynamicComment> batchQueryByNewIds(List<Integer> newsIds){
        List<TbComment>  tbCommentList = newsCommentService.batchQueryById(newsIds);
        List<RespDynamicComment> lstResult = new ArrayList<>();
        if (tbCommentList != null && tbCommentList.size() > 0){
            List<Integer> listUid = ImmutableSet.copyOf(this.getListUid(tbCommentList)).asList();
            Map<Integer,TbUser> tbUserMap = userQueryService.batchQueryTbUserMap(listUid);
            for (TbComment tbComment : tbCommentList){
                RespDynamicComment respDynamicComment = new RespDynamicComment();
                respDynamicComment.setId(tbComment.getId());
                respDynamicComment.setDynamicDetailsId(tbComment.getModuleId());
                respDynamicComment.setContent(tbComment.getContent());
                respDynamicComment.setCreateTime(tbComment.getCreateTime().getTime());
                respDynamicComment.setType(tbComment.getType());
                respDynamicComment.setToCommentId(tbComment.getToCommentId());
                RespUserEntity user = userDynamicService.convertTbUserToRespUserEntity(tbUserMap.get(tbComment.getUid()));
                respDynamicComment.setUser(user);
                if (tbComment.getTouid() != null && tbComment.getTouid().intValue() != 0 ){
                    RespUserEntity toUser = userDynamicService.convertTbUserToRespUserEntity(tbUserMap.get(tbComment.getTouid()));
                    respDynamicComment.setToUser(toUser);
                }else{
                    RespUserEntity respUserEntity = new RespUserEntity();
                    respUserEntity.setIcon(new ImageObj());
                    respDynamicComment.setToUser(respUserEntity);
                }
                lstResult.add(respDynamicComment);
            }
        }
        return lstResult;
    }



    //list 按属性生成一个新的 list
    public ImmutableMultiset<Integer> getListUid(List<TbComment> lst) {
        return ImmutableMultiset.copyOf(Lists.transform(lst, new Function<TbComment, Integer>() {
                    @Override
                    public Integer apply(TbComment input) {
                        return input.getUid();
                    }
                }
        ));
    }
}
